Loading Resources at Runtime
Manual     Reference     Scripting   
Unity Manual > Advanced > Loading Resources at Runtime

Loading Resources at Runtime

There are a number of scenarios where you want to include some kind of asset in your finished game that is not included inside the primary game executable: standalone, web player or your iOS/Android application. For example, one scenario is to slim down a web player's download size, and dynamically download and instantiate individual objects or items. This way only the necessary objects are loaded by the end-user. Another scenario might be for downloadable content like additional weapons, environments, characters, or even full levels.

In Unity Pro/iOS Advanced/Android Advanced, you can choose between two options for accomplishing these tasks: Asset Bundles, and Resource Folders. Non-Pro/Advanced license holders can use Resource Folders only.

Asset Bundles (Unity Pro-only/iOS Advanced/Android Advanced licenses only)

An Asset Bundle is an external collection of assets. You can have many Asset Bundles and therefore many different external collections of assets. These files exist outside of the built Unity player, usually sitting on a web server for end-users to access dynamically.

To build an Asset Bundle, you call BuildPipeline.BuildAssetBundle() from inside an Editor script. In the arguments, you specify an array of Objects to be included in the built file, along with some other options. This will build a file that you can later load dynamically in the runtime by using AssetBundle.Load().

Resource Folders

Resource Folders are collections of assets that are included in the built Unity player, but are not necessarily linked to any GameObject in the Inspector.

To put anything into a Resource Folder, you simply create a new folder inside the Project View, and name the folder "Resources". You can have multiple Resource Folders organized differently in your Project. Whenever you want to load an asset from one of these folders, you call Resources.Load().

If your target deployable is a Streaming Web Player, you can define which scene will include everything in your Resource Folders. You do this in the Player Settings, accessible via Edit->Project Settings->Player. Set the First Streamed Level With Resources parameter, and all assets in your Resource Folders will be loaded when this level streams in to the end-user.

Note:

All assets found in the Resources folders and their dependencies are stored in a file called resources.assets. If an asset is already used by another level it is stored in the .sharedAssets file for that level. The Edit -> PlayerSettings 'First Streamed Level' setting determines the level at which the resources.assets will be collected and included in the build.

If a level prior to "First streamed Level" is including an asset in a Resource folder, the asset will be stored in assets for that level. if it is included afterwards, the level will reference the asset from the "resources.assets" file.

Only assets that are in the Resources folder can be accessed through Resources.Load. However many more assets might end up in the "resources.assets" file since they are dependencies. (For example a Material in the Resources folder might reference a Texture outside of the Resources folder)

Resource Unloading

You can unload resources of an AssetBundle by calling AssetBundle.Unload(). If you pass true for the unloadAllLoadedObjects parameter, both the objects held internally by the AssetBundle and the ones loaded from the AssetBundle using AssetBundle.Load() will be destroyed and memory used by the bundle will be released.

Sometimes you may prefer to load an AssetBundle, instantiate the objects desired and release the memory used up by the bundle while keeping the objects around. The benefit is that you free up memory for other tasks, for instance loading another AssetBundle. In this scenario you would pass false as the parameter. After the bundle is destroyed you will not be able to load objects from it any more.

If you want to destroy scene objects loaded using Resources.Load() prior to loading another level, call Object.Destroy() on them. To release assets, use Resources.UnloadUnusedAssets().

Page last updated: 2010-09-25